﻿using DaInterFace;
using DaInterFace.SYN;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace E01
{
    public class A35 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, DaInterFace.SerialType st, string parameter)
        {
            E01A35P P = (E01A35P)handler.Serial.Deserialize(st, typeof(E01A35P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[4];
                sps[0] = new SqlParameter("@EASID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@EASNumber", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@EASName", SqlDbType.NVarChar, 50);
                sps[2].Value = P.Name;
                sps[3] = new SqlParameter("@Model", SqlDbType.NVarChar, 50);
                if (P.Model == null)
                {
                    sps[3].Value = "";
                }
                else
                {
                    sps[3].Value = P.Model;
                }

                String sql = "Begin tran  declare @flag int  declare @ID int set @flag = 0 declare @result int set @result = 0" +
                             " select @ID = isnull(FItemID,-10) from t_ICItem where F_102 = @EASID " +
                             " if(@ID is not null and @ID > 0) " +
                             " Begin " +
                             "      update t_ICItem set FModel = @Model,FShortNumber = @EASNumber,FNumber = @EASNumber,FName = @EASName,F_102=@EASID,F_103=@EASName,F_104=@EASNumber where FItemID = @ID " +
	                         "      set @flag = @flag + @@ERROR " +
                             "      update t_Item set FName = @EASName,FNumber=@EASNumber,FShortNumber=@EASNumber,FFullNumber=@EASNumber  where FItemID = @ID " +
                             "      set @flag = @flag + @@ERROR  set @result = 1" +
                             "   END " +
                             "   Else " +
                             "   Begin " +
                             "       EXEC GetICMaxNum 't_Item',@ID OUTPUT " +
	                         "       INSERT INTO t_ICItem  " +
	                         "       (FHelpCode,FModel,FAuxClassID,FErpClsID,FTypeID,FUnitGroupID,FUnitID,FOrderUnitID,FSaleUnitID,FProductUnitID,FStoreUnitID,FSecUnitID,FSecCoefficient,FDefaultLoc,FSPID, " +
	                         "       FSource,FQtyDecimal,FLowLimit,FHighLimit,FSecInv,FUseState,FIsEquipment,FEquipmentNum,FIsSparePart,FFullName,FApproveNo,FAlias,FOrderRector,FPOHighPrice,FPOHghPrcMnyType, " +
	                         "       FWWHghPrc,FWWHghPrcMnyType,FSOLowPrc,FSOLowPrcMnyType,FIsSale,FProfitRate,FOrderPrice,FSalePrice,FIsSpecialTax,FISKFPeriod,FKFPeriod,FStockTime,FBatchManager,FBookPlan,FBeforeExpire,FCheckCycUnit,FOIHighLimit,FOILowLimit, " +
	                         "       FSOHighLimit,FSOLowLimit,FInHighLimit,FInLowLimit,FTrack,FPlanPrice,FPriceDecimal,FAcctID,FSaleAcctID,FCostAcctID,FAPAcctID,FAdminAcctID,FGoodSpec,FTaxRate,FCostProject,FIsSNManage,FCBRestore,FNote,FPlanTrategy, " +
	                         "       FPlanMode,FOrderTrategy,FFixLeadTime,FLeadTime,FTotalTQQ,FOrderInterVal,FQtyMin,FQtyMax,FBatchAppendQty,FOrderPoint,FBatFixEconomy,FBatChangeEconomy,FRequirePoint,FPlanPoint,FDefaultRoutingID,FDefaultWorkTypeID,FProductPrincipal,FPlanner,FIsBackFlush,FBackFlushStockID, " +
	                         "       FBackFlushSPID,FPutInteger,FDailyConsume,FMRPCon,FMRPOrder,FChartNumber,FIsKeyItem,FGrossWeight,FNetWeight,FMaund,FLength,FWidth,FHeight,FSize,FCubicMeasure,FStandardCost,FCBAppendRate,FCBAppendProject,FCostBomID,FCBRouting,FStdBatchQty,FStandardManHour,FStdPayRate,FChgFeeRate, " +
	                         "       FStdFixFeeRate,FOutMachFee,FPieceRate,FPOVAcctID,FPIVAcctID,FMCVAcctID,FPCVAcctID,FSLAcctID,FCAVAcctID,FInspectionLevel,FProChkMde,FWWChkMde,FSOChkMde,FWthDrwChkMde,FStkChkMde,FOtherChkMde,FStkChkPrd,FStkChkAlrm,FInspectionProject,FIdentifier,FVersion,FNameEn, " +
	                         "       FModelEn,FHSNumber,FExportRate,FFirstUnit,FSecondUnit,FImpostTaxRate,FConsumeTaxRate,FFirstUnitRate,FSecondUnitRate,FIsManage,FManageType,FLenDecimal,FCubageDecimal,FWeightDecimal,FIsCharSourceItem,FCtrlType,FCtrlStraregy,FContainerName,FKanBanCapability,FShortNumber, " +
	                         "       FNumber,FName,FParentID,FItemID,F_102,F_103,F_104)  " +
                             "       VALUES (NULL,@Model,0,1,0,149,150,150,150,150,150,0,0,0,0,0,4,0,1000,0,341,0,NULL,0, " +
	                         "       NULL,NULL,NULL,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,76,0,2,1000,1000,1000,0, " +
	                         "       0,0,17,0,0,1,NULL,321,14036,331,0,0,0,0,1,10000,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,0,NULL,0,0,0, " +
	                         "       0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,352,352,352,352,352,352,352,9999,0,0,0,NULL,NULL,NULL,0, " +
	                         "       0,NULL,NULL,0,0,0,0,0,0,2,4,2,0,14039,0,NULL,1,@EASNumber,@EASNumber,@EASName,0,@ID,@EASID,@EASName,@EASNumber) " +
	                         "       set @flag = @flag + @@ERROR " +
	                         "       INSERT INTO t_Item  " +
	                         "       (FItemID,FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted)  " +
	                         "       VALUES (@ID,4,0,1,@EASName,@EASNumber,@EASNumber,@EASNumber,1,cast(NEWID() as varchar(36)),0) " +
                             "       set @flag = @flag + @@ERROR set @result = 2" +
                             "   End " +
                             "   if @flag = 0 " +
                             "   Begin " +
	                         "       commit tran " +
                             "       select 1,@ID,@result" +
                             "   End " +
                             "   else " +
                             "   Begin " +
	                         "       rollBack " +
                             "       select 2,@ID,@result " +
                             "   End";



                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "1")
                    {
                        if (rs.Data.Tables[0].Rows[0][2].ToString() == "1")
                        {
                            result.Result = EnumResult.Update;
                            result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                            result.Message = String.Format("物料同步完成:{0},{1}", P.Number, result.Result.ToString());
                        }
                        else
                        {
                            result.Result = EnumResult.Insert;
                            result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                            result.Message = String.Format("物料同步完成:{0},{1}", P.Number, result.Result.ToString());
                        }
                    }
                    else 
                    {
                        result.Result = EnumResult.None;
                        result.Message = rs.Message;
                        result.Message = String.Format("物料同步失败:{0}", rs.Message);
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = rs.Message;
                    result.Message = String.Format("物料同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = ex.Message;
            }
        }
    }

    /// <summary>
    /// 物料
    /// </summary>
    public class E01A35P
    {
        public String Number = String.Empty;         //三方代码
        public String ID = String.Empty;             //三方内码
        public String Name = String.Empty;           //三方名称
        public String BaseNo;                        //账套编号
        public String Model;                         //规格型号
        public String stockID;                       //仓库三方内码
            
    }
}
